package com.xin.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xin.constants.EmployeeConstants;
import com.xin.constants.ShopConstants;
import com.xin.entity.crm.EmployEntity;
import com.xin.entity.shop.ShopEntity;
import com.xin.entity.shop.ShopOperateLogEntity;
import com.xin.exception.MyException;
import com.xin.mapper.ShopMapper;
import com.xin.mapper.ShopOperateLogMapper;
import com.xin.service.EmployeeService;
import com.xin.service.ShopOperateLogService;
import com.xin.service.ShopService;
import com.xin.utils.EmailUtils;
import com.xin.utils.HtmUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 店铺审核日志
 */
@Service
public class ShopOperateLogServiceImpl extends ServiceImpl<ShopOperateLogMapper, ShopOperateLogEntity> implements ShopOperateLogService {

    @Resource
    private ShopService shopService;
    @Resource
    private ShopMapper shopMapper;
    @Resource
    private RedisTemplate redisTemplate;

    @Autowired
    private JavaMailSender javaMailSender;
    @Resource
    private EmployeeService employeeService;
    /**
     * 店铺审核
     * @param shopOperateLogEntity
     * @param request
     * @return
     */
    @Override
    @Transactional
    public Integer add(ShopOperateLogEntity shopOperateLogEntity, HttpServletRequest request) {
        Long shopId = shopOperateLogEntity.getShopId();
        if (shopId==null||shopId.longValue()==0) throw new MyException("店铺信息不存在");
        ShopEntity shopEntity = this.shopService.getBaseMapper().selectById(shopId);
        if (shopEntity==null) throw new MyException("店铺信息不存在");
        if (shopEntity.getState()==2) throw new MyException("店铺审核已通过不可重复审核");
        else if (shopEntity.getState()==3) throw new MyException("店铺已经激活不可以重复审核");
        //获取用户email
        String shopAdminEmail = this.shopMapper.getShopAdminEmail(shopId);
        //查看店铺审核次数
        Integer auditCount = this.baseMapper.selectCount(new QueryWrapper<ShopOperateLogEntity>().eq("shop_id", shopId));
        if (auditCount>=3){
            //发送邮件告知用户
            String template = HtmUtils.getToManAuditsEmailTemplate("尊敬的用户您好，由于您多次提交都未审核通过，系统无法继续执行后面流程");
            EmailUtils.sendEmailByTemplate("审核驳回",javaMailSender,shopAdminEmail,template);
            throw new MyException("店铺审核次数已达到上限,以发送邮件告知用户");
        }

        String token = request.getHeader("token");//获取用户token
        String auth = EmployeeConstants.LOGIN + token;
        Object o = redisTemplate.boundValueOps(auth).get();
        EmployEntity employEntity = (EmployEntity) o;
        shopOperateLogEntity.setOperateId(employEntity.getId());
        //设置审核时间
        shopOperateLogEntity.setOperateTime(new Date());
        //更改店铺状态

        //发送邮件给用户
        //审核通过
        if (shopOperateLogEntity.getOperateType()==2){
            //生成用户的token
            String shopToken = UUID.randomUUID().toString();
            redisTemplate.boundValueOps(ShopConstants.ACTIVE_SHOP+shopToken).set(shopId);
            //发送邮件通告用户可以登录账号了
            EmailUtils.sendEmail("激活账号",javaMailSender,shopAdminEmail,shopToken);
            //审核通过
            shopEntity.setState(2);
        }
        //审核被驳回
        else if (shopOperateLogEntity.getOperateType()==3){
            String s = UUID.randomUUID().toString();
            redisTemplate.boundValueOps(ShopConstants.UPDATE_SHOP+s).set(shopOperateLogEntity.getShopId());
            //让邮件中携带token
            String noPass = HtmUtils.getNoPass(shopOperateLogEntity.getNote(),s);
            EmailUtils.send(javaMailSender,noPass,shopAdminEmail,"审核驳回");
            shopEntity.setState(4);
        }
        int insert = this.baseMapper.insert(shopOperateLogEntity);
        this.shopMapper.updateById(shopEntity);
        return insert;
    }

    /**
     * 查看当前店铺的审核记录
     * @param shopId
     * @return
     */
    @Override
    public List<ShopOperateLogEntity> findByShopId(Long shopId) {

        return this.baseMapper.findByShopId(shopId);
    }
}
